/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.jaxws;

import java.net.URI;

import javax.xml.ws.addressing.AddressingProperties;
import javax.xml.ws.addressing.AttributedURI;
import javax.xml.ws.addressing.EndpointReference;
import javax.xml.ws.addressing.JAXWSAConstants;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;

/**
 * @author boudyacho
 */
@Name("inboundAddressing")
@Scope(ScopeType.EVENT)
@Install(value = false, precedence = Install.FRAMEWORK)
@AutoCreate
@BypassInterceptors
public class InboundAddressing {

	@Logger
	private Log log;

	private AddressingProperties properties;

	@Create
	public void create() throws Exception {
		properties = (AddressingProperties) MessageContexts.instance()
				.getMessageContext().get(
						JAXWSAConstants.SERVER_ADDRESSING_PROPERTIES_INBOUND);
		if (properties == null) {
			throw new RuntimeException("Cannot obtain AddressingProperties");
		}
		log.debug("Created: {0}", this);
	}

	// ---------------------------------------------------------------

	public String getMessageId() {
		AttributedURI uri = properties.getMessageID();
		if (uri == null) {
			throw new RuntimeException("Cannot obtain correlation message id");
		}

		log.debug("Message ID:  {0}", uri);
		return uri.getURI().toString();
	}

	public String getReplyTo() {
		EndpointReference replyTo = properties.getReplyTo();
		log.debug("Reply To: {0}", replyTo.getAddress());
		return replyTo.getAddress().getURI().toString();
	}

	public String getRelatesTo() throws Exception {
		URI uri = properties.getRelatesTo()[0].getID();
		if (uri == null) {
			throw new RuntimeException("Cannot obtain correlation message id");
		}
		log.debug("Relates To:  {0}", uri);
		return uri.toString();
	}

	// ---------------------------------------------------------------

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	public static InboundAddressing instance() {
		return (InboundAddressing) Component
				.getInstance(InboundAddressing.class);
	}
}
